﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Data;

namespace Gmantis.Controls.DataGrid
{
    internal class DataGridView
    {
        // Fields
        private Point _draggingRelativePosition;
        private DispatcherTimer _draggingTimer;
        private bool _dragStartedInSelectedCell;
        private Point _dragStartOffset;
        private Point _dragStartRelativePosition;
        private DataGridCellsRange[] _initialSelectionRanges;
        private bool _isDraggingSelection;
        private DragHelper _rowsPanelDragHelper;
        private bool _startingCellSelectedWhileDragging;
        private DataGridColumn currentMouseOverColumn;
        private DataGridRow currentMouseOverRow;
        public static Style SimpleCellStyle = PlatformIndependent.LoadXaml<Style>("<Style\n    TargetType='c1DataGrid:DataGridCellPresenter'\n    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'\n    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'\n    xmlns:c1DataGrid='clr-namespace:C1.Silverlight.DataGrid;assembly=C1.Silverlight.DataGrid.5'\n    >\n    <Setter Property='Template'>\n        <Setter.Value>\n            <ControlTemplate TargetType='c1DataGrid:DataGridCellPresenter'>\n                <ContentPresenter />\n            </ControlTemplate>\n        </Setter.Value>\n    </Setter>\n</Style>\n");

        // Events
        public event EventHandler<DataGridColumnEventArgs> ColumnActualWidthChanged;

        public event EventHandler<DataGridRowEventArgs> RowActualHeightChanged;

        // Methods
        public DataGridView(DataGrid dataGrid, DataGridModel model)
        {
            EventHandler handler = null;
            EventHandler<DataGridColumnEventArgs> handler2 = null;
            EventHandler<DataGridColumnEventArgs> handler3 = null;
            EventHandler<DataGridColumnEventArgs> handler4 = null;
            EventHandler<DataGridColumnEventArgs> handler5 = null;
            NotifyCollectionChangedEventHandler handler6 = null;
            EventHandler<DataGridRowEventArgs> handler7 = null;
            EventHandler handler8 = null;
            EventHandler<DataGridRowsAddedEventArgs> handler9 = null;
            EventHandler<DataGridRowsDeletedEventArgs> handler10 = null;
            EventHandler<DataGridRowEventArgs> handler11 = null;
            EventHandler<DataGridRowEventArgs> handler12 = null;
            EventHandler<DataGridRowEventArgs> handler13 = null;
            EventHandler<DataGridRowEventArgs> handler14 = null;
            EventHandler<DataGridColumnEventArgs> handler15 = null;
            EventHandler<DataGridSelectionChangedEventArgs> handler16 = null;
            EventHandler<PropertyChangedEventArgs<DataGridCell>> handler17 = null;
            EventHandler<PropertyChangedEventArgs<DataGridRow>> handler18 = null;
            EventHandler<PropertyChangedEventArgs<DataGridCell>> handler19 = null;
            this._draggingTimer = new DispatcherTimer();
            this._dragStartRelativePosition = new Point();
            this._dragStartOffset = new Point();
            this._draggingRelativePosition = new Point();
            this.DataGrid = dataGrid;
            this.Model = model;
            this.Properties = new DataGridViewProperties();
            this.ViewColumns = new DataGridViewColumnsCollection(this);
            this.ViewRows = new DataGridViewRowsCollection(this);
            this.InputHandlingStrategy = new DataGridNewInputHandlingStrategy(dataGrid);
            if (handler == null)
            {
                handler = delegate(object s, EventArgs e)
                {
                    if (this.IsLoaded)
                    {
                        this.ScrollManager.ResetScroll();
                    }
                };
            }
            this.Model.ItemsSourceChanged += handler;
            if (handler2 == null)
            {
                handler2 = delegate(object s, DataGridColumnEventArgs e)
                {
                    ViewColumn column;
                    if (this.ViewColumns.TryGetValue(e.Column, out column))
                    {
                        column.Unload(true, true, true);
                        this.ViewColumns.Remove(e.Column);
                    }
                };
            }
            this.Model.Columns.BeforeRemoveColumn += handler2;
            if (handler3 == null)
            {
                handler3 = (s, e) => this.InvalidatePanelsMeasure();
            }
            this.Model.Columns.AfterAddColumn += handler3;
            if (handler4 == null)
            {
                handler4 = (s, e) => this.InvalidatePanelsMeasure();
            }
            this.Model.Columns.ColumnReordered += handler4;
            if (handler5 == null)
            {
                handler5 = (s, e) => this.InvalidatePanelsMeasure();
            }
            this.Model.Columns.ColumnVisibilityChanged += handler5;
            if (handler6 == null)
            {
                handler6 = delegate(object s, NotifyCollectionChangedEventArgs e)
                {
                    this.InvalidatePanelsMeasure();
                    this.UpdateCellsAreaMargin();
                    this.InvalidateGroupingPresenter();
                };
            }
            this.Model.Rows.CollectionChanged += handler6;
            if (handler7 == null)
            {
                handler7 = delegate(object s, DataGridRowEventArgs e)
                {
                    ViewRow row;
                    if (this.ViewRows.TryGetValue(e.Row, out row))
                    {
                        row.Unload(true, true, true);
                        this.ViewRows.Remove(e.Row);
                    }
                };
            }
            this.Model.Rows.BeforeRowUnloaded += handler7;
            if (handler8 == null)
            {
                handler8 = delegate(object s, EventArgs e)
                {
                    this.InvalidateGroupingPresenter();
                    this.UpdateCellsAreaMargin();
                };
            }
            this.Model.GroupManager.GroupChanged += handler8;
            if (handler9 == null)
            {
                handler9 = delegate(object s, DataGridRowsAddedEventArgs e)
                {
                    this.InvalidatePanelsMeasure();
                    this.UpdateRowBackgroundAndForeground();
                };
            }
            this.Model.RowsAdded += handler9;
            if (handler10 == null)
            {
                handler10 = delegate(object s, DataGridRowsDeletedEventArgs e)
                {
                    this.InvalidatePanelsMeasure();
                    this.UpdateRowBackgroundAndForeground();
                };
            }
            this.Model.RowsDeleted += handler10;
            if (handler11 == null)
            {
                handler11 = (s, e) => this.InvalidatePanelsMeasure();
            }
            this.Model.GroupRowVisibilityChanged += handler11;
            if (handler12 == null)
            {
                handler12 = delegate(object s, DataGridRowEventArgs e)
                {
                    e.Row.ViewRow.UpdateLines();
                    e.Row.ViewRow.ClearCachedData();
                    this.InvalidatePanelsMeasure();
                };
            }
            this.Model.RowDetailsVisibilityChanged += handler12;
            if (handler13 == null)
            {
                handler13 = (s, e) => this.InvalidatePanelsMeasure();
            }
            this.Model.RowVisibilityChanged += handler13;
            if (handler14 == null)
            {
                handler14 = delegate(object s, DataGridRowEventArgs e)
                {
                    e.Row.ViewRow.ClearCachedData();
                    this.InvalidatePanelsMeasure();
                };
            }
            this.Model.RowHeightChanged += handler14;
            if (handler15 == null)
            {
                handler15 = delegate(object s, DataGridColumnEventArgs e)
                {
                    e.Column.ViewColumn.ClearCachedData();
                    this.InvalidatePanelsMeasure();
                };
            }
            this.Model.ColumnWidthChanged += handler15;
            if (handler16 == null)
            {
                handler16 = (s, e) => this.UpdateSelection();
            }
            this.Model.SelectionChanged += handler16;
            if (handler17 == null)
            {
                handler17 = (s, e) => this.UpdateEditingCell(e.NewValue, e.OldValue);
            }
            this.Model.CurrentEditingCellChanged += handler17;
            if (handler18 == null)
            {
                handler18 = (s, e) => this.UpdateEditingRow(e.NewValue, e.OldValue);
            }
            this.Model.CurrentEditingRowChanged += handler18;
            if (handler19 == null)
            {
                handler19 = delegate(object s, PropertyChangedEventArgs<DataGridCell> e)
                {
                    this.UpdateCurrentCell();
                    this.UpdateValidationState();
                };
            }
            this.Model.CurrentCellChanged += handler19;
            this._draggingTimer.Tick += new EventHandler(this.OnDraggingTimerTick);
        }

        public bool BeginCellEdit()
        {
            return this.BeginCellEdit((RoutedEventArgs)null);
        }

        public bool BeginCellEdit(DataGridCell cell)
        {
            this.EditingEventArgs = null;
            return (this.Model.SetCurrentCell(cell) && this.Model.BeginCellEdit());
        }

        public bool BeginCellEdit(RoutedEventArgs editingEventArgs)
        {
            this.EditingEventArgs = editingEventArgs;
            return this.Model.BeginCellEdit();
        }

        public bool BeginCellEdit(int rowIndex, int columnIndex)
        {
            return this.BeginCellEdit(this.DataGrid[rowIndex, columnIndex]);
        }

        internal bool BeginNewRow()
        {
            try
            {
                if (this.Model.NewRowManager.ShowNewRow)
                {
                    DataGridColumn firstEditableColumn = this.Model.GetFirstEditableColumn();
                    if (firstEditableColumn != null)
                    {
                        this.BeginCellEdit(this.DataGrid[this.Model.NewRowManager.NewRow, firstEditableColumn]);
                    }
                }
            }
            catch (DataGridFinalizeEditingException)
            {
                return false;
            }
            return false;
        }

        public void ClearMouseOver()
        {
            if ((this.currentMouseOverColumn != null) || (this.currentMouseOverRow != null))
            {
                this.currentMouseOverRow = null;
                this.currentMouseOverColumn = null;
                this.UpdateMouseOver();
            }
        }

        public DataGridVerticalFreezingSeparatorPresenter CreateVerticalFreezingSeparatorPresenter()
        {
            DataGridVerticalFreezingSeparatorPresenter target = new DataGridVerticalFreezingSeparatorPresenter
            {
                Cursor = Cursors.SizeWE,
                View = this
            };
            if ((this.DataGrid.VerticalFreezingSeparatorStyle != null) && (target.Style == null))
            {
                target.Style = this.DataGrid.VerticalFreezingSeparatorStyle;
            }
            target.SetBinding<DataGrid>(Control.BackgroundProperty, this.DataGrid, dg => dg.HeaderBackground);
            target.SetBinding<DataGrid>(Control.BorderBrushProperty, this.DataGrid, dg => dg.BorderBrush);
            target.SetBinding<DataGrid>(DataGridVerticalFreezingSeparatorPresenter.PressedBrushProperty, this.DataGrid, dg => dg.PressedBrush);
            target.SetBinding<DataGrid>(DataGridVerticalFreezingSeparatorPresenter.MouseOverBrushProperty, this.DataGrid, dg => dg.MouseOverBrush);
            target.SetBinding<DataGridViewProperties>(DataGridVerticalFreezingSeparatorPresenter.IsDraggingProperty, this.Properties, dgp => dgp.IsDraggingVerticalFreezingSeparator);
            target.SetBinding(DataGridVerticalFreezingSeparatorPresenter.ForceMouseOverProperty, new Binding().From<DataGridViewProperties>(this.Properties, x => x.IsOverVerticalFreezingSeparator));
            Binding binding = new Binding
            {
                Converter = CustomConverter.Create((value, type, parameter, culture) => (((DataGridColumnFreezing)value) & DataGridColumnFreezing.Left) == DataGridColumnFreezing.Left)
            };
            target.SetBinding(Control.IsEnabledProperty, binding.From<DataGrid>(this.DataGrid, x => x.CanUserFreezeColumns));
            DragHelper helper = new DragHelper(target, DragHelperMode.TranslateXY, 1.0, true, false, false, false);
            int currentFrozenColumnCount = 0;
            helper.DragStarted += delegate(object s, DragStartedEventArgs e)
            {
                this.ScrollManager.ScrollHorizontally(0.0);
                currentFrozenColumnCount = this.Model.FrozenColumnCount;
                this.Properties.IsDraggingVerticalFreezingSeparator = true;
            };
            helper.DragDelta += delegate(object s, DragDeltaEventArgs e)
            {
                int num1 = currentFrozenColumnCount;
                int num2 = Math.Sign(e.CumulativeTranslation.X);
                double num3 = Math.Abs(e.CumulativeTranslation.X);
                double num4 = 0.0;
                DataGridColumn[] orderedColumns = this.Model.Columns.OrderedColumns;
                for (int j = currentFrozenColumnCount + ((num2 > 0) ? 0 : -1); (num2 > 0) ? (j < orderedColumns.Length) : (j >= 0); j += num2)
                {
                    DataGridColumn column = orderedColumns[j];
                    ViewColumn viewColumn = column.ViewColumn;
                    if (num3 <= (num4 + (viewColumn.RenderWidth / 2.0)))
                    {
                        break;
                    }
                    if (num2 > 0)
                    {
                        if (!column.IsFrozen && (viewColumn.RenderWidth >= this.ScrollManager.ScrollingWidth))
                        {
                            break;
                        }
                        num1 = j + 1;
                    }
                    else
                    {
                        num1 = j;
                    }
                    num4 += viewColumn.RenderWidth;
                }
                if (this.DataGrid.FrozenColumnCount != num1)
                {
                    this.DataGrid.FrozenColumnCount = num1;
                    this.ScrollManager.ScrollHorizontally(0.0);
                }
            };
            helper.DragCompleted += delegate(object s, DragCompletedEventArgs e)
            {
                this.Properties.IsDraggingVerticalFreezingSeparator = false;
            };
            return target;
        }

        public bool EndEdit(bool commitChanges, bool exitEditingMode)
        {
            this.EditingEventArgs = null;
            if (this.Model.FocusAndEditingManager.CurrentEditingCell == null)
            {
                return true;
            }
            DataGridRow row = this.Model.FocusAndEditingManager.CurrentEditingCell.Row;
            if (commitChanges)
            {
                bool flag = this.Model.CommitCellEdit(exitEditingMode, false);
                if (!flag)
                {
                }
                return flag;
            }
            return this.Model.CancelCellEdit(false, exitEditingMode);
        }

        internal bool EndEditRow(bool commitChanges)
        {
            if (commitChanges)
            {
                return this.Model.CommitRowEdit(false);
            }
            return this.Model.CancelRowEdit(false);
        }

        internal bool EndNewRow(bool commitChanges)
        {
            return this.EndEditRow(commitChanges);
        }

        private void FinalizeRowsPanel()
        {
            if (this._rowsPanelDragHelper != null)
            {
                this._rowsPanelDragHelper.FinalizeDrag();
            }
            this._rowsPanelDragHelper = null;
        }

        public DataGridCell GetCellFromFrameworkElement(FrameworkElement element)
        {
            foreach (FrameworkElement element2 in element.GetAllParents())
            {
                DataGridCellPresenter presenter = element2 as DataGridCellPresenter;
                if ((presenter != null) && (presenter.Row.DataGrid == this.DataGrid))
                {
                    return this.Model.GetCell(presenter.ViewRow.Row, presenter.ViewColumn.Column);
                }
            }
            return null;
        }

        public DataGridCell GetCellFromPoint(Point intersectingPoint, bool useVisualTree = true)
        {
            if (useVisualTree)
            {
                foreach (UIElement element in this.DataGrid.HitTestProjectionWorkaround(intersectingPoint))
                {
                    DataGridCellPresenter presenter = element as DataGridCellPresenter;
                    if ((presenter != null) && (presenter.DataGrid == this.DataGrid))
                    {
                        return this.DataGrid[presenter.Row, presenter.Column];
                    }
                }
            }
            else
            {
                Point point = this.RowsPanel.TransformToVisual(null).Inverse.Transform(intersectingPoint);
                RelativePosition relativePositionHorizontally = this.ScrollManager.GetRelativePositionHorizontally(point.X, null);
                RelativePosition relativePositionVertically = this.ScrollManager.GetRelativePositionVertically(point.Y, null);
                if ((relativePositionVertically.Index >= 0) && (relativePositionHorizontally.Index >= 0))
                {
                    return this.Model.GetCell(relativePositionVertically.Index, relativePositionHorizontally.Index);
                }
            }
            return null;
        }

        internal DataGridCellPresenter GetCellPresenter(DataGridRow row, DataGridColumn column)
        {
            ViewRow row2;
            ViewColumn column2;
            if (this.ViewRows.TryGetValue(row, out row2) && this.ViewColumns.TryGetValue(column, out column2))
            {
                return row2.GetCellPresenter(column2, false);
            }
            return null;
        }

        public DataGridDetailsPresenter GetDetailFromPoint(Point intersectingPoint)
        {
            foreach (UIElement element in this.DataGrid.HitTestProjectionWorkaround(intersectingPoint))
            {
                DataGridDetailsPresenter presenter = element as DataGridDetailsPresenter;
                if ((presenter != null) && (presenter.DataGrid == this.DataGrid))
                {
                    return presenter;
                }
            }
            return null;
        }

        internal IEnumerable<ViewColumn> GetFrozenAndScrollingColumns(int columnIndex, GeneratorDirection horizontalDirection)
        {
            for (int i = 0; i < this.Model.FrozenColumnCount; i++)
            {
                DataGridColumn iteratorVariable1 = this.Model.Columns.OrderedColumns[i];
                if (iteratorVariable1.Visibility == Visibility.Visible)
                {
                    yield return iteratorVariable1.ViewColumn;
                }
            }
            int fromColumnIndex = Math.Max(this.Model.FrozenColumnCount, columnIndex);
            foreach (DataGridColumn iteratorVariable3 in this.Model.Columns.GetVisibleColumnsFromIndex(fromColumnIndex, horizontalDirection))
            {
                if (iteratorVariable3.DisplayIndex < this.Model.FrozenColumnCount)
                {
                    break;
                }
                yield return iteratorVariable3.ViewColumn;
            }
        }

        public IEnumerable<ViewRow> GetFrozenAndScrollingRows(int rowIndex, GeneratorDirection verticalDirection)
        {
            int iteratorVariable0 = this.Model.Rows.Count - 1;
            for (int i = 0; i < this.Model.FrozenTopRowsCount; i++)
            {
                DataGridRow iteratorVariable2 = this.Model.Rows[i];
                if (iteratorVariable2.IsVisible(false))
                {
                    yield return iteratorVariable2.ViewRow;
                }
            }
            for (int j = 0; j < this.Model.FrozenBottomRowsCount; j++)
            {
                DataGridRow iteratorVariable4 = this.Model.Rows[iteratorVariable0 - j];
                if (iteratorVariable4.IsVisible(false))
                {
                    yield return iteratorVariable4.ViewRow;
                }
            }
            int fromRowIndex = Math.Max(this.Model.FrozenTopRowsCount, Math.Min(iteratorVariable0 - this.Model.FrozenBottomRowsCount, rowIndex));
            foreach (DataGridRow iteratorVariable6 in this.Model.Rows.GetVisibleRowsFromIndex(fromRowIndex, verticalDirection, false))
            {
                int index = iteratorVariable6.Index;
                if ((index < this.Model.FrozenTopRowsCount) || (index > (iteratorVariable0 - this.Model.FrozenBottomRowsCount)))
                {
                    break;
                }
                yield return iteratorVariable6.ViewRow;
            }
        }

        public double GetFrozenHeight()
        {
            double num = 0.0;
            for (int i = 0; i < this.Model.FrozenTopRowsCount; i++)
            {
                ViewRow viewRow = this.Model.Rows[i].ViewRow;
                if (viewRow.Row.IsVisible(false))
                {
                    num += viewRow.GetTotalRenderHeight();
                }
            }
            for (int j = this.Model.Rows.Count - this.Model.FrozenBottomRowsCount; j < this.Model.Rows.Count; j++)
            {
                ViewRow row2 = this.Model.Rows[j].ViewRow;
                if (row2.Row.IsVisible(false))
                {
                    num += row2.GetTotalRenderHeight();
                }
            }
            return num;
        }

        public double GetFrozenWidth(bool includeIndentWidth = false)
        {
            double verticalFreezingSeparatorWidth = this.Model.VerticalFreezingSeparatorWidth;
            if (includeIndentWidth)
            {
                verticalFreezingSeparatorWidth += this.GetRenderLeadingColumnWidth();
            }
            for (int i = 0; i < this.Model.FrozenColumnCount; i++)
            {
                DataGridColumn column = this.Model.Columns.OrderedColumns[i];
                if (column.Visibility == Visibility.Visible)
                {
                    verticalFreezingSeparatorWidth += column.ViewColumn.RenderWidth;
                }
            }
            return verticalFreezingSeparatorWidth;
        }

        public double GetMinFrozenWidth()
        {
            double verticalFreezingSeparatorWidth = this.Model.VerticalFreezingSeparatorWidth;
            for (int i = 0; i < this.Model.FrozenColumnCount; i++)
            {
                DataGridColumn column = this.Model.Columns.OrderedColumns[i];
                if (column.Visibility == Visibility.Visible)
                {
                    verticalFreezingSeparatorWidth += column.ViewColumn.MinRenderWidth;
                }
            }
            return verticalFreezingSeparatorWidth;
        }

        public double GetRenderLeadingColumnWidth()
        {
            if (this.Model.GroupManager.GroupCount == 0)
            {
                return 0.0;
            }
            if (this.DataGrid.LeadingColumnWidth.IsAbsolute)
            {
                return this.DataGrid.LeadingColumnWidth.Value;
            }
            return (this.Model.GroupManager.GroupCount * this.DataGrid.IndentWidth);
        }

        public DataGridRow GetRowFromFrameworkElement(FrameworkElement element)
        {
            foreach (FrameworkElement element2 in element.GetAllParents())
            {
                DataGridRowPresenter presenter = element2 as DataGridRowPresenter;
                if ((presenter != null) && (presenter.Row.DataGrid == this.DataGrid))
                {
                    return presenter.Row;
                }
                DataGridDetailsPresenter presenter2 = element2 as DataGridDetailsPresenter;
                if ((presenter2 != null) && (presenter2.Row.DataGrid == this.DataGrid))
                {
                    return presenter2.Row;
                }
            }
            return null;
        }

        internal ViewColumn GetViewColumn(DataGridColumn column, bool forceCreate)
        {
            ViewColumn column2;
            if (forceCreate)
            {
                return this.ViewColumns[column];
            }
            if (this.ViewColumns.TryGetValue(column, out column2))
            {
                return column2;
            }
            return null;
        }

        internal ViewRow GetViewRow(DataGridRow row, bool forceCreate)
        {
            ViewRow row2;
            if (forceCreate)
            {
                return this.ViewRows[row];
            }
            if (this.ViewRows.TryGetValue(row, out row2))
            {
                return row2;
            }
            return null;
        }

        internal void HideFocus()
        {
            this.UpdateCurrentCell();
        }

        private void InitializeColumnsHeaderPanel(DataGridColumnsHeaderPanel columnsHeaderPanel, Popup popup)
        {
            this.ColumnsHeaderPanel = columnsHeaderPanel;
            if (this.ColumnsHeaderPanel != null)
            {
                this.ColumnsHeaderPanel.View = this;
                this.ColumnsHeaderPanel.SetBinding(FrameworkElement.HeightProperty, new Binding().From<DataGrid>(this.DataGrid, x => x.ColumnHeaderHeight));
                Binding binding = new Binding
                {
                    Converter = CustomConverter.Create((value, type, parameter, culture) => ((((DataGridHeadersVisibility)value) & DataGridHeadersVisibility.Column) == DataGridHeadersVisibility.Column) ? Visibility.Visible : Visibility.Collapsed)
                };
                this.ColumnsHeaderPanel.SetBinding(UIElement.VisibilityProperty, binding.From<DataGrid>(this.DataGrid, x => x.HeadersVisibility));
            }
            this.ColumnsHeaderManager = new DataGridColumnsHeaderManager(this, popup);
        }

        internal void InitializeDragHelper()
        {
            if (this._rowsPanelDragHelper != null)
            {
                this._rowsPanelDragHelper.FinalizeDrag();
                this._rowsPanelDragHelper = null;
            }
            if ((this.RowsPanel != null) && this.DataGrid.CanUserSelectByDragging)
            {
                bool handledEventsToo = true;
                this._rowsPanelDragHelper = new DragHelper(this.RowsPanel, DragHelperMode.Inertia | DragHelperMode.TranslateRailX | DragHelperMode.TranslateRailY | DragHelperMode.TranslateX | DragHelperMode.TranslateY, 1.0, false, handledEventsToo, false, false);
                this._rowsPanelDragHelper.DragStarted += new EventHandler<DragStartedEventArgs>(this.OnRowsPanelDragStarted);
                this._rowsPanelDragHelper.DragDelta += new EventHandler<DragDeltaEventArgs>(this.OnRowsPanelDragMove);
                this._rowsPanelDragHelper.DragCompleted += new EventHandler<DragCompletedEventArgs>(this.OnRowsPanelDragEnd);
            }
        }

        private void InitializeGroupingPresenter(DataGridGroupingPresenter groupingPresenter)
        {
            this.GroupingPresenter = groupingPresenter;
            if (this.GroupingPresenter != null)
            {
                this.GroupingPresenter.View = this;
                Binding binding = new Binding
                {
                    Converter = new VisibilityConverter()
                };
                this.GroupingPresenter.SetBinding(UIElement.VisibilityProperty, binding.From<DataGrid>(this.DataGrid, x => x.ActualShowGroupingPanel));
                this.GroupingPresenter.SetBinding<DataGrid>(FrameworkElement.StyleProperty, this.DataGrid, x => x.GroupingPanelStyle);
                this.GroupingPresenter.SetBinding<DataGrid>(Control.BackgroundProperty, this.DataGrid, x => x.GroupingPanelBackground);
                this.GroupingPresenter.SetBinding<DataGrid>(Control.ForegroundProperty, this.DataGrid, x => x.GroupingPanelForeground);
                this.GroupingPresenter.SetBinding<DataGrid>(Control.BorderBrushProperty, this.DataGrid, x => x.BorderBrush);
                this.GroupingPresenter.InvalidateGroups();
            }
        }

        private void InitializeHorizontalScrollBar(ScrollBar horizontalScrollBar)
        {
            this.HorizontalScrollBar = horizontalScrollBar;
            this.HorizontalScrollBar.Scroll += (s, e) => this.ScrollManager.ScrollHorizontally(e.NewValue);
            this.HorizontalScrollBar.SetBinding<DataGrid>(FrameworkElement.StyleProperty, this.DataGrid, x => x.ScrollBarStyle);
            this.HorizontalScrollBar.SetBinding<DataGrid>(Control.BackgroundProperty, this.DataGrid, x => x.HeaderBackground);
            this.HorizontalScrollBar.SetBinding<DataGrid>(Control.ForegroundProperty, this.DataGrid, x => x.HeaderForeground);
            this.HorizontalScrollBar.SetBinding<DataGrid>(Control.BorderBrushProperty, this.DataGrid, x => x.BorderBrush);
            this.HorizontalScrollBar.SetBinding<DataGrid>(ClearStyleExtensions.MouseOverBrushProperty, this.DataGrid, x => x.MouseOverBrush);
            this.HorizontalScrollBar.SetBinding<DataGrid>(ClearStyleExtensions.PressedBrushProperty, this.DataGrid, x => x.PressedBrush);
        }

        private void InitializeRowsHeaderPanel(DataGridRowsHeaderPanel rowsHeaderPanel)
        {
            this.RowsHeaderPanel = rowsHeaderPanel;
            if (this.RowsHeaderPanel != null)
            {
                this.RowsHeaderPanel.View = this;
                this.RowsHeaderPanel.MouseWheel += new MouseWheelEventHandler(this.OnRowsHeaderPanelMouseWheel);
                this.RowsHeaderPanel.SetBinding(FrameworkElement.WidthProperty, new Binding().From<DataGrid>(this.DataGrid, x => x.RowHeaderWidth));
                Binding binding = new Binding
                {
                    Converter = CustomConverter.Create((value, type, parameter, culture) => ((((DataGridHeadersVisibility)value) & DataGridHeadersVisibility.Row) == DataGridHeadersVisibility.Row) ? Visibility.Visible : Visibility.Collapsed)
                };
                this.RowsHeaderPanel.SetBinding(UIElement.VisibilityProperty, binding.From<DataGrid>(this.DataGrid, x => x.HeadersVisibility));
            }
        }

        private void InitializeRowsPanel(DataGridRowsPanel rowsPanel)
        {
            this.RowsPanel = rowsPanel;
            this.RowsPanel.View = this;
            this.RowsPanel.SizeChanged += new SizeChangedEventHandler(this.OnRowsPanelSizeChanged);
            this.RowsPanel.MouseWheel += new MouseWheelEventHandler(this.OnRowsPanelMouseWheel);
            this.RowsPanel.MouseLeftButtonDown += new MouseButtonEventHandler(this.OnRowsPanelMouseLeftButtonDown);
            TapHelper helper = new TapHelper(this.RowsPanel, false);
            helper.Tapped += new EventHandler<TappedEventArgs>(this.OnRowsPanelTapped);
            this.InitializeDragHelper();
        }

        private void InitializeTopLeftCornerButton(Button topLeftCorner)
        {
            RoutedEventHandler handler = null;
            this.TopLeftCorner = topLeftCorner;
            if (this.TopLeftCorner != null)
            {
                this.TopLeftCorner.IsTabStop = false;
                if (handler == null)
                {
                    handler = delegate(object s, RoutedEventArgs e)
                    {
                        this.DataGrid.Selection.SelectAll();
                        this.DataGrid.Focus();
                    };
                }
                this.TopLeftCorner.Click += handler;
                Binding binding = new Binding
                {
                    Converter = CustomConverter.Create((value, type, parameter, culture) => ((DataGridSelectionMode)value).IsMultiMode())
                };
                this.TopLeftCorner.SetBinding(Control.IsEnabledProperty, binding.From<DataGrid>(this.DataGrid, x => x.SelectionMode));
                Binding binding2 = new Binding
                {
                    Converter = CustomConverter.Create((value, type, parameter, culture) => (((DataGridHeadersVisibility)value) == DataGridHeadersVisibility.All) ? Visibility.Visible : Visibility.Collapsed)
                };
                this.TopLeftCorner.SetBinding(UIElement.VisibilityProperty, binding2.From<DataGrid>(this.DataGrid, x => x.HeadersVisibility));
                this.TopLeftCorner.SetBinding<DataGrid>(Control.BackgroundProperty, this.DataGrid, x => x.HeaderBackground);
                this.TopLeftCorner.SetBinding<DataGrid>(Control.BorderBrushProperty, this.DataGrid, x => x.BorderBrush);
                this.TopLeftCorner.SetBinding<DataGrid>(Control.ForegroundProperty, this.DataGrid, x => x.MouseOverBrush);
            }
        }

        private void InitializeVerticalScrollBar(ScrollBar verticalScrollBar, Popup popup)
        {
            this.VerticalScrollBar = verticalScrollBar;
            this.VerticalScrollBar.Scroll += delegate(object s, System.Windows.Controls.Primitives.ScrollEventArgs e)
            {
                switch (e.ScrollEventType)
                {
                    case ScrollEventType.SmallDecrement:
                        this.ScrollManager.ScrollSteps(-1);
                        return;

                    case ScrollEventType.SmallIncrement:
                        this.ScrollManager.ScrollSteps(1);
                        return;

                    case ScrollEventType.LargeDecrement:
                        this.ScrollManager.PageUp();
                        return;

                    case ScrollEventType.LargeIncrement:
                        this.ScrollManager.PageDown();
                        return;

                    case ScrollEventType.ThumbPosition:
                    case ScrollEventType.First:
                    case ScrollEventType.Last:
                        break;

                    case ScrollEventType.ThumbTrack:
                        {
                            if (this.DataGrid.ScrollMode != DataGridScrollMode.Deferred)
                            {
                                this.ScrollManager.ScrollVertically(e.NewValue);
                                break;
                            }
                            if (this.DataGrid.ScrollPreviewRowTemplate == null)
                            {
                                break;
                            }
                            double offset = 0.0;
                            int rowIndexFromScrollPosition = this.ScrollManager.GetRowIndexFromScrollPosition(e.NewValue, out offset);
                            if (offset < -4.0)
                            {
                                rowIndexFromScrollPosition = Math.Min((int)(rowIndexFromScrollPosition + 1), (int)(this.Model.Rows.Count - 1));
                            }
                            FrameworkElement element = this.DataGrid.ScrollPreviewRowTemplate.LoadContent() as FrameworkElement;
                            if (element == null)
                            {
                                break;
                            }
                            Canvas canvas = new Canvas();
                            element.DataContext = this.Model.Rows[rowIndexFromScrollPosition];
                            canvas.Children.Add(element);
                            Point point = verticalScrollBar.TransformToVisual(this.DataGrid).Transform(new Point());
                            element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                            Canvas.SetLeft(element, point.X - element.DesiredSize.Width);
                            Canvas.SetTop(element, point.Y + (((verticalScrollBar.ActualHeight - element.DesiredSize.Height) / verticalScrollBar.Maximum) * e.NewValue));
                            popup.Child = canvas;
                            popup.IsOpen = true;
                            return;
                        }
                    case ScrollEventType.EndScroll:
                        if (this.DataGrid.ScrollMode != DataGridScrollMode.Deferred)
                        {
                            break;
                        }
                        popup.Child = null;
                        popup.IsOpen = false;
                        this.ScrollManager.ScrollVertically(e.NewValue);
                        return;

                    default:
                        return;
                }
            };
            this.VerticalScrollBar.SetBinding<DataGrid>(FrameworkElement.StyleProperty, this.DataGrid, x => x.ScrollBarStyle);
            this.VerticalScrollBar.SetBinding<DataGrid>(Control.BackgroundProperty, this.DataGrid, x => x.HeaderBackground);
            this.VerticalScrollBar.SetBinding<DataGrid>(Control.ForegroundProperty, this.DataGrid, x => x.HeaderForeground);
            this.VerticalScrollBar.SetBinding<DataGrid>(Control.BorderBrushProperty, this.DataGrid, x => x.BorderBrush);
            this.VerticalScrollBar.SetBinding<DataGrid>(ClearStyleExtensions.MouseOverBrushProperty, this.DataGrid, x => x.MouseOverBrush);
            this.VerticalScrollBar.SetBinding<DataGrid>(ClearStyleExtensions.PressedBrushProperty, this.DataGrid, x => x.PressedBrush);
        }

        private void InvalidateGroupingPresenter()
        {
            if (this.GroupingPresenter != null)
            {
                this.GroupingPresenter.InvalidateGroups();
            }
        }

        public void InvalidatePanelsMeasure()
        {
            if (this.IsLoaded)
            {
                this.RowsPanel.InvalidateRowsMeasure();
                this.RowsPanel.InvalidateMeasure();
                if (this.RowsHeaderPanel != null)
                {
                    this.RowsHeaderPanel.InvalidateMeasure();
                }
                if (this.ColumnsHeaderPanel != null)
                {
                    this.ColumnsHeaderPanel.InvalidateMeasure();
                }
            }
        }

        internal bool IsMouseBetween(DataGridColumn left, DataGridColumn right)
        {
            if (this.currentMouseOverColumn == null)
            {
                return false;
            }
            if (this.currentMouseOverRow == null)
            {
                return ((left.DisplayIndex <= this.currentMouseOverColumn.DisplayIndex) && (this.currentMouseOverColumn.DisplayIndex <= right.DisplayIndex));
            }
            DataGridCellsRange range = this.Viewport.Range(this.DataGrid[this.currentMouseOverRow, this.currentMouseOverColumn]);
            if (right.DisplayIndex < range.TopLeftCell.Column.DisplayIndex)
            {
                return false;
            }
            if (range.BottomRightCell.Column.DisplayIndex < left.DisplayIndex)
            {
                return false;
            }
            return true;
        }

        internal bool IsMouseBetween(DataGridRow top, DataGridRow bottom)
        {
            if (this.currentMouseOverRow == null)
            {
                return false;
            }
            if (this.currentMouseOverColumn == null)
            {
                return ((top.Index <= this.currentMouseOverRow.Index) && (this.currentMouseOverRow.Index <= bottom.Index));
            }
            DataGridCellsRange range = this.Viewport.Range(this.DataGrid[this.currentMouseOverRow, this.currentMouseOverColumn]);
            if (bottom.Index < range.TopLeftCell.Row.Index)
            {
                return false;
            }
            if (range.BottomRightCell.Row.Index < top.Index)
            {
                return false;
            }
            return true;
        }

        public bool IsMouseOverCell(DataGridRow row, DataGridColumn column)
        {
            if (this.Model.SelectionManager.Selection.Mode.IsRowSelectionMode())
            {
                return this.IsMouseOverRow(row);
            }
            if (this.Model.SelectionManager.Selection.Mode.IsColumnSelectionMode())
            {
                return this.IsMouseOverColumn(column);
            }
            return (this.IsMouseOverRow(row) && this.IsMouseOverColumn(column));
        }

        public bool IsMouseOverColumn(DataGridColumn column)
        {
            return this.IsMouseBetween(column, column);
        }

        public bool IsMouseOverRow(DataGridRow row)
        {
            return this.IsMouseBetween(row, row);
        }

        public void Load(DataGridRowsPanel rowsPanel, ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar, DataGridGroupingPresenter groupingPresenter, DataGridRowsHeaderPanel rowsHeaderPanel, DataGridColumnsHeaderPanel columnsHeaderPanel, Button topLeftCorner, Popup popup)
        {
            this.InitializeRowsPanel(rowsPanel);
            this.InitializeHorizontalScrollBar(horizontalScrollBar);
            this.InitializeVerticalScrollBar(verticalScrollBar, popup);
            this.InitializeGroupingPresenter(groupingPresenter);
            this.InitializeRowsHeaderPanel(rowsHeaderPanel);
            this.InitializeColumnsHeaderPanel(columnsHeaderPanel, popup);
            this.InitializeTopLeftCornerButton(topLeftCorner);
            this.ScrollManager = new DataGridScrollManager(this);
            this.ScrollManager.AvailableSizeChanged += delegate(object s, EventArgs a)
            {
                if (this.ColumnsHeaderPanel != null)
                {
                    this.ColumnsHeaderPanel.InvalidateMeasure();
                }
                this.RowsPanel.InvalidateRowsMeasure();
            };
            this.ScrollManager.HorizontalOffsetChanged += (s, a) => this.HorizontalScrollBar.Value = this.ScrollManager.HorizontalOffset;
            this.ScrollManager.VerticalOffsetChanged += (s, a) => this.VerticalScrollBar.Value = this.ScrollManager.VerticalOffset;
            this.Viewport = new DataGridViewport(this);
            this.DataGrid.FrozenColumnCountChanged += (s, e) => this.ScrollManager.ScrollHorizontally(0.0);
            this.DataGrid.FrozenBottomRowsCountChanged += (s, e) => this.InvalidatePanelsMeasure();
            this.DataGrid.FrozenTopRowsCountChanged += (s, e) => this.InvalidatePanelsMeasure();
            this.UpdateCellsAreaMargin();
            this.IsLoaded = true;
        }

        internal void OnCellValueUpdated(DataGridCell cell)
        {
            if ((cell.Row != null) && (cell.Column != null))
            {
                ViewRow viewRow = cell.Row.ViewRow;
                ViewColumn viewColumn = cell.Column.ViewColumn;
                if ((viewRow != null) && (viewColumn != null))
                {
                    viewRow.Unload(false, true, false);
                }
            }
        }

        internal void OnColumnActualWidthChanged(ViewColumn viewColumn)
        {
            if (this.IsLoaded)
            {
                this.RowsPanel.InvalidateRowsMeasure();
                this.RowsPanel.InvalidateArrange();
                if (this.ColumnsHeaderPanel != null)
                {
                    this.ColumnsHeaderPanel.InvalidateMeasure();
                }
                if (this.ColumnActualWidthChanged != null)
                {
                    this.ColumnActualWidthChanged(viewColumn, new DataGridColumnEventArgs(viewColumn.Column));
                }
            }
        }

        internal void OnColumnResized(DataGridColumn column, double width)
        {
            this.DataGrid.OnColumnResized(new DataGridColumnEventArgs(column));
        }

        internal void OnColumnResizing(DataGridColumn column, double width)
        {
            this.DataGrid.OnColumnResizing(new DataGridColumnEventArgs(column));
        }

        private void OnDraggingTimerTick(object sender, EventArgs e)
        {
            if (this._isDraggingSelection)
            {
                double num5;
                int num = -1;
                int index = -1;
                int rowIndexFromScrollPosition = -1;
                int columnDisplayIndexFromScrollPosition = -1;
                RelativePosition relativePositionHorizontally = this.ScrollManager.GetRelativePositionHorizontally(this._dragStartRelativePosition.X, new double?(this._dragStartOffset.X));
                RelativePosition relativePositionVertically = this.ScrollManager.GetRelativePositionVertically(this._dragStartRelativePosition.Y, new double?(this._dragStartOffset.Y));
                RelativePosition position3 = this.ScrollManager.GetRelativePositionHorizontally(this._draggingRelativePosition.X, null);
                RelativePosition position4 = this.ScrollManager.GetRelativePositionVertically(this._draggingRelativePosition.Y, null);
                double num6 = (this.ScrollManager.FrozenLeftWidth + this.DataGrid.VerticalFreezingSeparatorWidth) + this.GetRenderLeadingColumnWidth();
                if (((relativePositionHorizontally.Type <= RelativePositionType.FirstFrozen) && (position3.Type > RelativePositionType.FirstFrozen)) && ((position3.Type < RelativePositionType.LastFrozen) && (this.ScrollManager.HorizontalOffset > 0.0)))
                {
                    this.ScrollManager.ScrollHorizontally(this.ScrollManager.HorizontalOffset - (this._draggingRelativePosition.X - num6));
                    this.RowsPanel.UpdateLayout();
                    columnDisplayIndexFromScrollPosition = this.ScrollManager.GetColumnDisplayIndexFromScrollPosition(this.ScrollManager.HorizontalOffset + (this._draggingRelativePosition.X - num6), out num5);
                    columnDisplayIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Columns.Count - 1, columnDisplayIndexFromScrollPosition));
                }
                else if (((relativePositionHorizontally.Type < RelativePositionType.LastFrozen) && (position3.Type >= RelativePositionType.LastFrozen)) && (this.ScrollManager.HorizontalOffset < (this.ScrollManager.ExtentWidth - this.ScrollManager.ScrollingWidth)))
                {
                    this.ScrollManager.ScrollHorizontally(this.ScrollManager.HorizontalOffset + (this._draggingRelativePosition.X - (this.ScrollManager.Viewport.Width - this.ScrollManager.FrozenRightWidth)));
                    this.RowsPanel.UpdateLayout();
                    columnDisplayIndexFromScrollPosition = this.ScrollManager.GetColumnDisplayIndexFromScrollPosition(this.ScrollManager.HorizontalOffset + (this._draggingRelativePosition.X - (this.ScrollManager.Viewport.Width - this.ScrollManager.FrozenRightWidth)), out num5);
                    columnDisplayIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Columns.Count - 1, columnDisplayIndexFromScrollPosition));
                }
                else if (((relativePositionHorizontally.Type > RelativePositionType.FirstFrozen) && (position3.Type <= RelativePositionType.FirstFrozen)) && (this.ScrollManager.HorizontalOffset > 0.0))
                {
                    this.ScrollManager.ScrollHorizontally(this.ScrollManager.HorizontalOffset - (num6 - this._draggingRelativePosition.X));
                    this.RowsPanel.UpdateLayout();
                    columnDisplayIndexFromScrollPosition = this.ScrollManager.GetColumnDisplayIndexFromScrollPosition(this.ScrollManager.HorizontalOffset + (this._draggingRelativePosition.X - num6), out num5);
                    columnDisplayIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Columns.Count - 1, columnDisplayIndexFromScrollPosition));
                }
                if (((relativePositionVertically.Type > RelativePositionType.FirstFrozen) && (position4.Type <= RelativePositionType.FirstFrozen)) && (this.ScrollManager.VerticalOffset > 0.0))
                {
                    this.ScrollManager.ScrollVertically(this.ScrollManager.VerticalOffset + (this._draggingRelativePosition.Y - this.ScrollManager.FrozenTopHeight));
                    this.RowsPanel.UpdateLayout();
                    rowIndexFromScrollPosition = this.ScrollManager.GetRowIndexFromScrollPosition(this.ScrollManager.VerticalOffset + (this._draggingRelativePosition.Y - this.ScrollManager.FrozenTopHeight), out num5);
                    rowIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Rows.Count - 1, rowIndexFromScrollPosition));
                }
                else if (((relativePositionVertically.Type <= RelativePositionType.FirstFrozen) && (position4.Type > RelativePositionType.FirstFrozen)) && ((position4.Type < RelativePositionType.LastFrozen) && (this.ScrollManager.VerticalOffset > 0.0)))
                {
                    this.ScrollManager.ScrollVertically(this.ScrollManager.VerticalOffset - (this._draggingRelativePosition.Y - this.ScrollManager.FrozenTopHeight));
                    this.RowsPanel.UpdateLayout();
                    rowIndexFromScrollPosition = this.ScrollManager.GetRowIndexFromScrollPosition(this.ScrollManager.VerticalOffset + (this._draggingRelativePosition.Y - this.ScrollManager.FrozenTopHeight), out num5);
                    rowIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Rows.Count - 1, rowIndexFromScrollPosition));
                }
                else if (((relativePositionVertically.Type < RelativePositionType.LastFrozen) && (position4.Type >= RelativePositionType.LastFrozen)) && (this.ScrollManager.VerticalOffset < (this.ScrollManager.ExtentHeight - this.ScrollManager.ScrollingHeight)))
                {
                    this.ScrollManager.ScrollVertically(this.ScrollManager.VerticalOffset + (this._draggingRelativePosition.Y - (this.ScrollManager.Viewport.Height - this.ScrollManager.FrozenBottomHeight)));
                    this.RowsPanel.UpdateLayout();
                    rowIndexFromScrollPosition = this.ScrollManager.GetRowIndexFromScrollPosition(this.ScrollManager.VerticalOffset + (this._draggingRelativePosition.Y - this.ScrollManager.FrozenTopHeight), out num5);
                    rowIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Rows.Count - 1, rowIndexFromScrollPosition));
                }
                else if (((relativePositionVertically.Type >= RelativePositionType.LastFrozen) && (position4.Type < RelativePositionType.LastFrozen)) && (this.ScrollManager.VerticalOffset < (this.ScrollManager.ExtentHeight - this.ScrollManager.ScrollingHeight)))
                {
                    this.ScrollManager.ScrollVertically(this.ScrollManager.VerticalOffset - (this._draggingRelativePosition.Y - (this.ScrollManager.Viewport.Height - this.ScrollManager.FrozenBottomHeight)));
                    this.RowsPanel.UpdateLayout();
                    rowIndexFromScrollPosition = this.ScrollManager.GetRowIndexFromScrollPosition(this.ScrollManager.VerticalOffset + (this._draggingRelativePosition.Y - this.ScrollManager.FrozenTopHeight), out num5);
                    rowIndexFromScrollPosition = Math.Max(0, Math.Min(this.Model.Rows.Count - 1, rowIndexFromScrollPosition));
                }
                DataGridCell fromCell = null;
                DataGridCell toCell = null;
                if (relativePositionVertically.Index >= 0)
                {
                    num = relativePositionVertically.Index;
                }
                else if (position4.Type < RelativePositionType.EmptyZone)
                {
                    int num7 = (this.Model.Rows.Count - this.Model.FrozenBottomRowsCount) - 1;
                    if (num7 >= 0)
                    {
                        num = num7;
                    }
                }
                else if ((position4.Type > RelativePositionType.EmptyZone) && (this.Model.FrozenBottomRowsCount > 0))
                {
                    num = this.Model.Rows.Count - this.Model.FrozenBottomRowsCount;
                }
                if (relativePositionHorizontally.Index >= 0)
                {
                    index = relativePositionHorizontally.Index;
                }
                else if (position3.Type < RelativePositionType.EmptyZone)
                {
                    int num8 = this.Model.Columns.Count - 1;
                    if (num8 >= 0)
                    {
                        index = num8;
                    }
                }
                if ((num >= 0) && (index >= 0))
                {
                    fromCell = this.Model.GetCell(this.Model.Rows[num], this.Model.Columns.OrderedColumns[index]);
                }
                if (rowIndexFromScrollPosition < 0)
                {
                    if (position4.Index >= 0)
                    {
                        rowIndexFromScrollPosition = position4.Index;
                    }
                    else if (position4.Type == RelativePositionType.EmptyZone)
                    {
                        if (relativePositionVertically.Type < RelativePositionType.EmptyZone)
                        {
                            int num9 = (this.Model.Rows.Count - this.Model.FrozenBottomRowsCount) - 1;
                            if (num9 >= 0)
                            {
                                rowIndexFromScrollPosition = num9;
                            }
                        }
                        else if ((relativePositionVertically.Type > RelativePositionType.EmptyZone) && (this.Model.FrozenBottomRowsCount >= 0))
                        {
                            rowIndexFromScrollPosition = this.Model.Rows.Count - this.Model.FrozenBottomRowsCount;
                        }
                    }
                    else if (position4.Type == RelativePositionType.BeforeFirstFrozen)
                    {
                        if (this.Model.Rows.Count > 0)
                        {
                            rowIndexFromScrollPosition = 0;
                        }
                    }
                    else if ((position4.Type == RelativePositionType.AfterLastFrozen) && (this.Model.Rows.Count > 0))
                    {
                        rowIndexFromScrollPosition = this.Model.Rows.Count - 1;
                    }
                }
                if (columnDisplayIndexFromScrollPosition < 0)
                {
                    if (position3.Index >= 0)
                    {
                        columnDisplayIndexFromScrollPosition = position3.Index;
                    }
                    else if (position3.Type == RelativePositionType.EmptyZone)
                    {
                        if (relativePositionHorizontally.Type < RelativePositionType.EmptyZone)
                        {
                            int num10 = this.Model.Columns.Count - 1;
                            if (num10 >= 0)
                            {
                                columnDisplayIndexFromScrollPosition = num10;
                            }
                        }
                    }
                    else if (position3.Type == RelativePositionType.BeforeFirstFrozen)
                    {
                        if (this.Model.Columns.Count > 0)
                        {
                            columnDisplayIndexFromScrollPosition = 0;
                        }
                    }
                    else if ((position3.Type == RelativePositionType.AfterLastFrozen) && (this.Model.Columns.Count > 0))
                    {
                        columnDisplayIndexFromScrollPosition = this.Model.Columns.Count - 1;
                    }
                }
                if (((columnDisplayIndexFromScrollPosition >= 0) && (columnDisplayIndexFromScrollPosition < this.Model.Columns.OrderedColumns.Length)) && ((rowIndexFromScrollPosition >= 0) && (rowIndexFromScrollPosition < this.Model.Rows.Count)))
                {
                    toCell = this.Model.GetCell(this.Model.Rows[rowIndexFromScrollPosition], this.Model.Columns.OrderedColumns[columnDisplayIndexFromScrollPosition]);
                }
                if ((fromCell != null) && (toCell != null))
                {
                    if (KeyboardUtil.Ctrl)
                    {
                        DataGridSelection selection = this.DataGrid.Selection;
                        selection.BeginUpdate();
                        selection.Clear();
                        foreach (DataGridCellsRange range in this._initialSelectionRanges)
                        {
                            selection.Add(range);
                        }
                        selection.Add(fromCell, toCell);
                        selection.EndUpdate();
                    }
                    else
                    {
                        this.Model.SelectionManager.Select(fromCell, toCell, false, false);
                    }
                }
                else
                {
                    this.Model.SelectionManager.Clear();
                }
                this.ClearMouseOver();
            }
        }

        internal void OnFilterLoading(DataGridColumnEditableValueEventArgs<IDataGridFilter> e)
        {
            this.DataGrid.OnFilterLoading(e);
        }

        internal void OnFilterOpened(DataGridColumnValueEventArgs<IDataGridFilter> e)
        {
            this.DataGrid.OnFilterOpened(e);
        }

        internal void OnRowActualHeightChanged(ViewRow viewRow)
        {
            if (this.RowsHeaderPanel != null)
            {
                this.RowsHeaderPanel.InvalidateMeasure();
            }
            if (this.RowActualHeightChanged != null)
            {
                this.RowActualHeightChanged(viewRow, new DataGridRowEventArgs(viewRow.Row));
            }
        }

        internal void OnRowResized(DataGridRow row, double height)
        {
            this.DataGrid.OnRowResized(new DataGridRowEventArgs(row));
        }

        internal void OnRowResizing(DataGridRow row, double height)
        {
            this.DataGrid.OnRowResizing(new DataGridRowEventArgs(row));
        }

        private void OnRowsHeaderPanelMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            double verticalOffset = this.ScrollManager.VerticalOffset;
            this.ScrollManager.ScrollSteps((int)(-e.Delta / 120));
            this.RowsHeaderPanel.UpdateLayout();
            e.Handled = !(verticalOffset == this.ScrollManager.VerticalOffset);
        }

        private void OnRowsPanelDragEnd(object sender, DragCompletedEventArgs e)
        {
            this._startingCellSelectedWhileDragging = false;
            if ((e.PointerDeviceType != PointerDeviceType.Touch) || this._dragStartedInSelectedCell)
            {
                DataGridSelectionDragEventArgs args = new DataGridSelectionDragEventArgs(e);
                this.DataGrid.OnSelectionDragCompleted(args);
                if (this._isDraggingSelection)
                {
                    this._isDraggingSelection = false;
                    this._draggingTimer.Stop();
                }
            }
        }

        private void OnRowsPanelDragMove(object sender, DragDeltaEventArgs e)
        {
            if ((e.PointerDeviceType != PointerDeviceType.Touch) || this._dragStartedInSelectedCell)
            {
                if (e.IsInertial)
                {
                    e.Complete();
                }
                else
                {
                    DataGridSelectionDragEventArgs args = new DataGridSelectionDragEventArgs(e);
                    this.DataGrid.OnSelectionDragDelta(args);
                    if (this._isDraggingSelection)
                    {
                        this._draggingRelativePosition = e.GetPosition(this.RowsPanel);
                    }
                }
            }
            else
            {
                this.ScrollManager.ScrollHorizontally(this.ScrollManager.HorizontalOffset - e.DeltaTranslation.X);
                this.ScrollManager.ScrollVertically(this.ScrollManager.VerticalOffset - e.DeltaTranslation.Y);
            }
        }

        private void OnRowsPanelDragStarted(object sender, DragStartedEventArgs e)
        {
            this._dragStartRelativePosition = this.RowsPanel.TransformToVisual(null).Inverse.Transform(e.Origin);
            this._dragStartOffset = new Point(this.ScrollManager.HorizontalOffset, this.ScrollManager.VerticalOffset);
            RelativePosition relativePositionHorizontally = this.ScrollManager.GetRelativePositionHorizontally(this._dragStartRelativePosition.X, null);
            RelativePosition relativePositionVertically = this.ScrollManager.GetRelativePositionVertically(this._dragStartRelativePosition.Y, null);
            DataGridCell cell = ((relativePositionHorizontally.Index >= 0) && (relativePositionVertically.Index >= 0)) ? this.Model.GetCell(this.Model.Rows[relativePositionVertically.Index], this.Model.Columns.OrderedColumns[relativePositionHorizontally.Index]) : null;
            this._dragStartedInSelectedCell = (cell != null) && cell.IsSelected;
            if ((e.PointerDeviceType != PointerDeviceType.Touch) || this._dragStartedInSelectedCell)
            {
                DataGridSelectionDragStartedEventArgs args = new DataGridSelectionDragStartedEventArgs(e);
                this.DataGrid.OnSelectionDragStarted(args);
                if ((!args.Cancel && (this.DataGrid.Selection.Mode != DataGridSelectionMode.None)) && relativePositionVertically.IsOverCell)
                {
                    if (cell != null)
                    {
                        this._initialSelectionRanges = this.DataGrid.Selection.SelectedRanges.ToArray<DataGridCellsRange>();
                        if (!this.SelectCell(cell, e.OriginalEventArgs, false))
                        {
                            return;
                        }
                        this._startingCellSelectedWhileDragging = true;
                        this.UpdateFocus();
                    }
                    this._isDraggingSelection = true;
                    this._draggingTimer.Start();
                }
            }
        }

        private void OnRowsPanelMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell currentCell = this.Model.FocusAndEditingManager.CurrentCell;
            if (((currentCell != null) && currentCell.IsEditing) && ((currentCell.Presenter != null) && (currentCell.Presenter.CellContent != null)))
            {
                IList<FrameworkElement> allParents = (e.OriginalSource as FrameworkElement).GetAllParents();
                if (allParents.Contains(currentCell.Presenter.CellContent) || !allParents.Contains(this.DataGrid))
                {
                    e.Handled = true;
                }
            }
        }

        private void OnRowsPanelMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            double verticalOffset = this.ScrollManager.VerticalOffset;
            this.ScrollManager.ScrollSteps((int)(-e.Delta / 120));
            this.RowsPanel.UpdateLayout();
            e.Handled = !(verticalOffset == this.ScrollManager.VerticalOffset);
        }

        private void OnRowsPanelSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.DataGrid.Parent != null)
            {
                Point point = this.RowsPanel.TransformToVisual(this.DataGrid).Transform(new Point(0.0, 0.0));
                this.DataGrid.BodyRect = new Rect(point.X, point.Y, e.NewSize.Width, e.NewSize.Height);
            }
        }

        private void OnRowsPanelTapped(object sender, TappedEventArgs e)
        {
            DataGridCell currentCell = this.Model.FocusAndEditingManager.CurrentCell;
            if (((currentCell != null) && currentCell.IsEditing) && ((currentCell.Presenter != null) && (currentCell.Presenter.CellContent != null)))
            {
                IEnumerable<FrameworkElement> parents = (e.OriginalSource as FrameworkElement).GetParents(true);
                if (parents.Contains<FrameworkElement>(currentCell.Presenter.CellContent) || !parents.Contains<FrameworkElement>(this.DataGrid))
                {
                    return;
                }
            }
            Point point = e.GetPosition(this.RowsPanel);
            RelativePosition relativePositionHorizontally = this.ScrollManager.GetRelativePositionHorizontally(point.X, null);
            RelativePosition relativePositionVertically = this.ScrollManager.GetRelativePositionVertically(point.Y, null);
            if ((!this._startingCellSelectedWhileDragging && (relativePositionHorizontally.Index >= 0)) && ((relativePositionVertically.Index >= 0) && relativePositionVertically.IsOverCell))
            {
                DataGridCell cell = this.Model.GetCell(this.Model.Rows[relativePositionVertically.Index], this.Model.Columns.OrderedColumns[relativePositionHorizontally.Index]);
                if (this.SelectCell(cell, e.OriginalEventArgs, !KeyboardUtil.Ctrl && !KeyboardUtil.Shift))
                {
                    this.UpdateFocus();
                }
            }
        }

        public bool PageDown(bool changeSelection)
        {
            DataGridRow row = this.ScrollManager.PageDown();
            if (!changeSelection || (row == null))
            {
                return true;
            }
            if (this.Model.SetCurrentRow(row, false))
            {
                this.Model.SelectionManager.Select(row);
                return true;
            }
            return false;
        }

        public bool PageUp(bool changeSelection)
        {
            DataGridRow row = this.ScrollManager.PageUp();
            if (!changeSelection || (row == null))
            {
                return true;
            }
            if (this.Model.SetCurrentRow(row, false))
            {
                this.Model.SelectionManager.Select(row);
                return true;
            }
            return false;
        }

        public void Refresh(bool recycle = true, bool refreshRows = true, bool refreshCells = true, bool refreshColumnHeaders = true, bool refreshRowHeaders = true)
        {
            this.UnloadPresenters(recycle, refreshRows, refreshCells, refreshColumnHeaders, refreshRowHeaders);
            this.InvalidatePanelsMeasure();
        }

        internal bool RemoveRows(DataGridRow[] removedRows)
        {
            return this.Model.RemoveRows(removedRows);
        }

        internal bool RemoveRows(int[] rowIndexes)
        {
            List<DataGridRow> list = new List<DataGridRow>();
            foreach (int num in rowIndexes)
            {
                list.Add(this.Model.Rows[num]);
            }
            return this.Model.RemoveRows(list.ToArray());
        }

        public void ScrollIntoView()
        {
            if ((this.Model.FocusAndEditingManager.CurrentRow != null) && (this.Model.FocusAndEditingManager.CurrentColumn != null))
            {
                this.ScrollIntoView(this.Model.FocusAndEditingManager.CurrentRow, this.Model.FocusAndEditingManager.CurrentColumn);
            }
        }

        public void ScrollIntoView(FrameworkElement element)
        {
            element.TransformToVisual(null).Transform(new Point(0.0, 0.0));
            if ((this.GetRowFromFrameworkElement(element) != null) && !double.IsInfinity(this.ScrollManager.AvailableSize.Height))
            {
                DataGridCellPresenter presenter = element as DataGridCellPresenter;
                if ((presenter != null) && (presenter.DataGrid == this.DataGrid))
                {
                    DataGridCell cell = presenter.Cell;
                    if (cell != null)
                    {
                        this.ScrollIntoView(cell.Row, cell.Column);
                    }
                }
                else
                {
                    Point point = element.TransformToVisual(this.RowsPanel).Transform(new Point(0.0, 0.0));
                    Point point2 = element.TransformToVisual(this.RowsPanel).Transform(new Point(element.ActualWidth, element.ActualHeight));
                    if (point.X < 0.0)
                    {
                        this.ScrollManager.ScrollHorizontally(Math.Max((double)0.0, (double)(this.ScrollManager.HorizontalOffset + point.X)));
                    }
                    else if (point2.X > this.RowsPanel.ActualWidth)
                    {
                        this.ScrollManager.ScrollHorizontally(this.ScrollManager.HorizontalOffset + (point2.X - this.RowsPanel.ActualWidth));
                    }
                    if (point.Y < 0.0)
                    {
                        this.ScrollManager.ScrollVertically(Math.Max((double)0.0, (double)(this.ScrollManager.VerticalOffset + point.Y)));
                    }
                    else if (point2.Y > this.RowsPanel.ActualHeight)
                    {
                        this.ScrollManager.ScrollVertically(this.ScrollManager.VerticalOffset + (point2.Y - this.RowsPanel.ActualHeight));
                    }
                }
            }
        }

        public void ScrollIntoView(DataGridRow row, DataGridColumn column)
        {
            if (this.IsLoaded)
            {
                this.ScrollManager.ScrollIntoViewVertically(row.Index);
                this.ScrollManager.ScrollIntoViewHorizontally(column.DisplayIndex);
            }
        }

        public void ScrollIntoView(int rowIndex, int columnIndex)
        {
            this.ScrollIntoView(this.Model.Rows[rowIndex], this.Model.Columns[columnIndex]);
        }

        public void ScrollIntoView(object item, DataGridColumn column)
        {
            this.ScrollIntoView(this.Model.Rows[item], column);
        }

        internal bool SelectCell(DataGridCell cell, EventArgs e = null, bool enterEditMode = true)
        {
            try
            {
                if (cell.Presenter != null)
                {
                    cell = cell.Presenter.MergedRange.TopLeftCell;
                }
                bool flag = cell == this.DataGrid.CurrentCell;
                if (!cell.IsEditing)
                {
                    this.DataGrid.CurrentCell = cell;
                    if (this.DataGrid.CurrentCell == cell)
                    {
                        this.DataGrid.Model.SelectionManager.Select(cell);
                        if (((flag || this.DataGrid.CurrentCell.Row.IsEditing) || cell.Column.EditOnSelection) && (cell.Column.ActualIsEditable && enterEditMode))
                        {
                            this.DataGrid.BeginEdit(e as RoutedEventArgs);
                        }
                    }
                }
                return true;
            }
            catch (DataGridFinalizeEditingException)
            {
                return false;
            }
        }

        public void SetMouseOverCell(DataGridRow row, DataGridColumn column)
        {
            if ((this.currentMouseOverRow != row) || (this.currentMouseOverColumn != column))
            {
                this.currentMouseOverRow = row;
                this.currentMouseOverColumn = column;
                this.UpdateMouseOver();
            }
        }

        public void SetMouseOverColumn(DataGridColumn column)
        {
            if ((this.currentMouseOverColumn != column) || (this.currentMouseOverRow != null))
            {
                this.currentMouseOverRow = null;
                this.currentMouseOverColumn = column;
                this.UpdateMouseOver();
            }
        }

        public void SetMouseOverRow(DataGridRow row)
        {
            if ((this.currentMouseOverRow != row) || (this.currentMouseOverColumn != null))
            {
                this.currentMouseOverRow = row;
                this.currentMouseOverColumn = null;
                this.UpdateMouseOver();
            }
        }

        internal void ShowFocus()
        {
            this.UpdateCurrentCell();
        }

        public void Unload()
        {
            this.UnloadPresenters(false, true, true, true, true);
            this.FinalizeRowsPanel();
            this.IsLoaded = false;
        }

        public void UnloadCellsPresenters(ViewColumn viewColumn)
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.FinalizeCellPresenter(viewColumn);
            }
        }

        internal void UnloadGroupRowsPresenters(ViewColumn viewColumn)
        {
            if (this.Model.GroupManager.GroupedColumns.Contains<DataGridColumn>(viewColumn.Column))
            {
                foreach (ViewRow row in this.ViewRows)
                {
                    if (row.Row.Type == DataGridRowType.Group)
                    {
                        DataGridGroupRow row2 = row.Row as DataGridGroupRow;
                        if (row2.Column == viewColumn.Column)
                        {
                            row.Unload(true, true, true);
                        }
                    }
                }
            }
        }

        private void UnloadPresenters(bool recycle = true, bool refreshRow = true, bool refreshCells = true, bool refreshColumnHeaders = true, bool refreshRowHeaders = true)
        {
            if (this.IsLoaded)
            {
                if (!recycle)
                {
                    this.RowsPanel.Clear();
                    if (this.RowsHeaderPanel != null)
                    {
                        this.RowsHeaderPanel.Clear();
                    }
                    if (this.ColumnsHeaderPanel != null)
                    {
                        this.ColumnsHeaderPanel.Clear();
                    }
                }
                foreach (ViewRow row in this.ViewRows)
                {
                    row.Unload(refreshRow, refreshCells, refreshRowHeaders);
                }
                foreach (ViewColumn column in this.ViewColumns)
                {
                    column.Unload(refreshCells, refreshColumnHeaders, refreshCells);
                }
            }
        }

        internal void UpdateCellsAreaMargin()
        {
            this.Properties.CellsAreaMargin = new Thickness(this.GetRenderLeadingColumnWidth(), 0.0, 0.0, 0.0);
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateRowAreMargin();
            }
            if (this.ColumnsHeaderPanel != null)
            {
                this.ColumnsHeaderPanel.InvalidateMeasure();
            }
        }

        public void UpdateCurrentCell()
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateCurrentCell();
            }
        }

        private void UpdateEditingCell(DataGridCell newValue, DataGridCell oldValue)
        {
            if ((oldValue != null) && this.IsLoaded)
            {
                oldValue.Row.ViewRow.RemoveEditingCell(oldValue.Column.ViewColumn);
            }
            if ((newValue != null) && this.IsLoaded)
            {
                DataGridRow row = newValue.Row;
                ViewRow viewRow = row.ViewRow;
                if (row.Presenter != null)
                {
                    row.Presenter.UpdateLayout();
                }
                FrameworkElement editingElement = viewRow.SetEditingCell(newValue.Column.ViewColumn);
                if (((this.EditingEventArgs != null) && (editingElement != null)) && !newValue.Column.BeginEdit(editingElement, this.EditingEventArgs))
                {
                    this.EndEdit(false, true);
                }
            }
        }

        private void UpdateEditingRow(DataGridRow newValue, DataGridRow oldValue)
        {
            if ((oldValue != null) && this.IsLoaded)
            {
                ViewRow viewRow = oldValue.GetViewRow(false);
                if (viewRow != null)
                {
                    viewRow.UpdateEditingRow();
                }
            }
            if ((newValue != null) && this.IsLoaded)
            {
                ViewRow row2 = newValue.GetViewRow(false);
                if (row2 != null)
                {
                    row2.UpdateEditingRow();
                }
            }
        }

        private void UpdateFocus()
        {
            FrameworkElement keyboardFocusedElement = PlatformIndependent.GetKeyboardFocusedElement();
            DataGridCell currentCell = this.Model.FocusAndEditingManager.CurrentCell;
            if ((((keyboardFocusedElement != this.DataGrid) && (currentCell != null)) && (currentCell.Row != null)) && (((currentCell.Row.Presenter == null) || (keyboardFocusedElement == null)) || !keyboardFocusedElement.GetAllParents().Contains(currentCell.Row.Presenter)))
            {
                this.DataGrid.Focus();
            }
        }

        public bool UpdateHorizontalScrollBar()
        {
            double extentWidth = this.ScrollManager.ExtentWidth;
            double scrollingWidth = this.ScrollManager.ScrollingWidth;
            Visibility collapsed = Visibility.Collapsed;
            if (!double.IsInfinity(scrollingWidth))
            {
                double num3 = Math.Max(0.0, Math.Round((double)(extentWidth - scrollingWidth), 0));
                this.HorizontalScrollBar.Maximum = num3;
                this.HorizontalScrollBar.ViewportSize = scrollingWidth;
                this.HorizontalScrollBar.LargeChange = 200.0;
                this.HorizontalScrollBar.SmallChange = 50.0;
                collapsed = (num3 > 0.0) ? Visibility.Visible : Visibility.Collapsed;
            }
            this.HorizontalScrollBar.IsEnabled = true;
            switch (this.DataGrid.HorizontalScrollBarVisibility)
            {
                case ScrollBarVisibility.Disabled:
                    this.HorizontalScrollBar.IsEnabled = false;
                    collapsed = Visibility.Visible;
                    break;

                case ScrollBarVisibility.Hidden:
                    collapsed = Visibility.Collapsed;
                    break;

                case ScrollBarVisibility.Visible:
                    collapsed = Visibility.Visible;
                    break;
            }
            if (this.HorizontalScrollBar.Visibility != collapsed)
            {
                this.HorizontalScrollBar.Visibility = collapsed;
                return true;
            }
            return false;
        }

        internal void UpdateLines()
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateLines();
            }
        }

        private void UpdateMouseOver()
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateMouseOver();
            }
            foreach (ViewColumn column in this.ViewColumns)
            {
                column.UpdateMouseOver();
            }
        }

        internal void UpdateRowBackgroundAndForeground()
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateRowBackgroundAndForeground();
                row.UpdateRowDetailsBackgroundAndForeground();
            }
        }

        public bool UpdateScrollBars()
        {
            double extentHeight = this.ScrollManager.ExtentHeight;
            double scrollingHeight = this.ScrollManager.ScrollingHeight;
            Visibility collapsed = Visibility.Collapsed;
            if (!double.IsInfinity(scrollingHeight))
            {
                collapsed = (Math.Max(0.0, Math.Round((double)(extentHeight - scrollingHeight), 0)) > 0.0) ? Visibility.Visible : Visibility.Collapsed;
            }
            double extentWidth = this.ScrollManager.ExtentWidth;
            double scrollingWidth = this.ScrollManager.ScrollingWidth;
            Visibility visibility2 = Visibility.Collapsed;
            if (!double.IsInfinity(scrollingWidth))
            {
                visibility2 = (Math.Max(0.0, Math.Round((double)(extentWidth - scrollingWidth), 0)) > 0.0) ? Visibility.Visible : Visibility.Collapsed;
            }
            bool flag = false;
            if ((((this.DataGrid.VerticalScrollBarVisibility == ScrollBarVisibility.Auto) && (this.VerticalScrollBar.Visibility != collapsed)) && ((this.DataGrid.HorizontalScrollBarVisibility == ScrollBarVisibility.Auto) && (this.HorizontalScrollBar.Visibility != visibility2))) && (collapsed != visibility2))
            {
                if (collapsed == Visibility.Collapsed)
                {
                    return this.UpdateVerticalScrollBar();
                }
                return this.UpdateHorizontalScrollBar();
            }
            flag |= this.UpdateHorizontalScrollBar();
            return (flag | this.UpdateVerticalScrollBar());
        }

        public void UpdateSelection()
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateSelection();
            }
            foreach (ViewColumn column in this.ViewColumns)
            {
                column.UpdateSelection();
            }
        }

        public void UpdateValidationState()
        {
            foreach (ViewRow row in this.ViewRows)
            {
                row.UpdateValidationState();
            }
        }

        public bool UpdateVerticalScrollBar()
        {
            double extentHeight = this.ScrollManager.ExtentHeight;
            double scrollingHeight = this.ScrollManager.ScrollingHeight;
            Visibility collapsed = Visibility.Collapsed;
            if (!double.IsInfinity(scrollingHeight))
            {
                double num3 = Math.Max(0.0, Math.Round((double)(extentHeight - scrollingHeight), 0));
                this.VerticalScrollBar.Maximum = num3;
                this.VerticalScrollBar.ViewportSize = scrollingHeight;
                collapsed = (num3 > 0.0) ? Visibility.Visible : Visibility.Collapsed;
            }
            this.VerticalScrollBar.IsEnabled = true;
            switch (this.DataGrid.VerticalScrollBarVisibility)
            {
                case ScrollBarVisibility.Disabled:
                    this.VerticalScrollBar.IsEnabled = false;
                    collapsed = Visibility.Visible;
                    break;

                case ScrollBarVisibility.Hidden:
                    collapsed = Visibility.Collapsed;
                    break;

                case ScrollBarVisibility.Visible:
                    collapsed = Visibility.Visible;
                    break;
            }
            if (this.VerticalScrollBar.Visibility != collapsed)
            {
                this.VerticalScrollBar.Visibility = collapsed;
                return true;
            }
            return false;
        }

        // Properties
        internal DataGridColumnsHeaderManager ColumnsHeaderManager { get; set; }

        public DataGridColumnsHeaderPanel ColumnsHeaderPanel { get; private set; }

        public DataGrid DataGrid { get; private set; }

        private RoutedEventArgs EditingEventArgs { get; set; }

        public DataGridGroupingPresenter GroupingPresenter { get; private set; }

        public ScrollBar HorizontalScrollBar { get; private set; }

        public IDataGridInputHandlingStrategy InputHandlingStrategy { get; set; }

        public bool IsLoaded { get; private set; }

        public DataGridModel Model { get; private set; }

        public DataGridViewProperties Properties { get; private set; }

        public DataGridRowsHeaderPanel RowsHeaderPanel { get; private set; }

        public DataGridRowsPanel RowsPanel { get; set; }

        public DataGridScrollManager ScrollManager { get; private set; }

        public Button TopLeftCorner { get; private set; }

        public ScrollBar VerticalScrollBar { get; private set; }

        private DataGridViewColumnsCollection ViewColumns { get; set; }

        public DataGridViewport Viewport { get; private set; }

        private DataGridViewRowsCollection ViewRows { get; set; }
    }
}
